set -e


ss=`date +%F%H%M%S`
mkdir $ss

mount -o loop $1 $ss
mkdir $1_temp
cp -r $ss/* $1_temp

umount $ss

export DM_INSTALL_TMPDIR=$PWD/$ss
cd $1_temp
nohup sh ./DMInstall.bin -i & 2>&1


a=`cat nohup.out|wc -l`
while true
do
if [ $a -gt 10 ];then
break
fi
sleep 3
a=`cat nohup.out|wc -l`
done

cd ..
cp -r $DM_INSTALL_TMPDIR/DMInstall/source/*  $1_temp

rm -rf $ss

set +e
ps -ef|grep DMInstall|awk '{print $2}'|xargs kill -9
set -e

#get version
cd $1_temp/bin
ver_=`./disql -id|awk '{print $NF}'`
cd -
#delete noneed jar
rm -rf $DM_INSTALL_TMPDIR/tool/dropins/com.dameng/plugins/com.dameng.help_*.jar

mv $1_temp $ver_
mv $ver_/tool/dmagent $ver_/
sed -i 's#= http://192.168.2.10:8080/dem#= http://127.0.0.1:8080/dem   #g'  $ver_/dmagent/agent.ini

a='
bin
disql
disql.debug
dmfldr
dmfldr.debug
lib
libcrypto.so
libdisql_dll.a
libdisql_dll.so
libdisql_dll.so.debug
libdmcalc.a
libdmcalc.so
libdmcalc.so.debug
libdmclientlex.a
libdmclientlex.so
libdmclientlex.so.debug
libdmcvt.a
libdmcvt.so
libdmcvt.so.debug
libdmcyt.a
libdmcyt.so
libdmcyt.so.debug
libdmdpi.a
libdmdpi.so
libdmdpi.so.debug
libdmelog.a
libdmelog.so
libdmelog.so.debug
libdmmc.so
libdmmem.a
libdmmem.so
libdmmem.so.debug
libdmmout.a
libdmmout.so
libdmmout.so.debug
libdmos.a
libdmos.so
libdmos.so.debug
libdmstrt.a
libdmstrt.so
libdmstrt.so.debug
libdmutl.a
libdmutl.so
libdmutl.so.debug
libgeos_c.so
libgeos.so
libregex.so
libsnappy.so
libssl.so
libxerces-c-3.1.so
libxerces-c.so
libxqilla.so
libz.so
OPENSSL_VERSION
'
mkdir $ver_/client
for rs in $a
do
set +e
cp -r $ver_/bin/$rs $ver_/client
set -e
done

mkdir  $ver_/client/disql_conf
echo 'set autocommit on' >>$ver_/client/disql_conf/glogin.sql

echo OK!

mkdir WEB-INF
# below cannot fist null row
echo '<?xml version="1.0" encoding="UTF-8"?>

<ConnectPool>
        <Server>127.0.0.1</Server>
        <Port>5237</Port>
        <User>SYSDBA</User>
        <Password>SYSDBA</Password>
        <InitPoolSize>5</InitPoolSize>
        <CorePoolSize>10</CorePoolSize>
        <MaxPoolSize>500</MaxPoolSize>
        <KeepAliveTime>60</KeepAliveTime>
        <DbDriver></DbDriver>
        <DbTestStatement>select 1</DbTestStatement>
        <SSLDir>../sslDir/client_ssl/SYSDBA</SSLDir>
        <SSLPassword></SSLPassword>
</ConnectPool>
'>WEB-INF/db.xml

$ver_/jdk/bin/jar uvf $ver_/web/dem.war  WEB-INF/db.xml
rm -rf WEB-INF

tar zvfx $2 -C $ver_
apdir=$ver_/`echo $2|sed 's/.tar.gz//g'`
mv $ver_/web/dem.war $apdir/webapps

sed -i  's#Connector port="8080" protocol="HTTP/1.1"#Connector port="8080" protocol="HTTP/1.1" maxPostSize="-1"#g' $apdir/conf/server.xml
echo "JAVA_OPTS=\"-server -Xms256m -Xmx1024m -Djava.library.path=$apdir/dmbin\"" >>$apdir/bin/catalina.sh

cp -r $ver_/bin $apdir/dmbin

cd $apdir/dmbin
./dminit path=data buffer=100 port_num=5237
temps=`date +%F%H%M%S`
cp dmserver dmserver$temps
nohup ./dmserver$temps data/DAMENG/dm.ini &

sleep 3
flag=`cat nohup.out|grep 'SYSTEM IS READY'|wc -l`
while [ $flag -eq 0 ]
do
sleep 3
flag=`cat nohup.out|grep 'SYSTEM IS READY'|wc -l`
echo $flag" ready dmserver sleep skip!"
done


echo 'exit;' >>../../../$ver_/web/dem_init.sql
./disql SYSDBA/SYSDBA:5237 -c 'SET CHAR_CODE UTF8' \`../../../$ver_/web/dem_init.sql
kill `pidof dmserver$temps`
rm dmserver$temps
echo dmserver$temps
cd -


mv $ver_/dmagent $appdir


